﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class D05_Quaternion1 : MonoBehaviour
{
    public float upAngleLimt = 85;
    public float downAngleLimt = 75;
    private void Start()
    {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Confined;
    }

    private void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Cursor.visible = false;
        }

        //左右旋转
        float mouseX = Input.GetAxis("Mouse X");
        transform.parent.Rotate(Vector3.up * Time.deltaTime * 60f * mouseX, Space.World);

        float mouseY = Input.GetAxis("Mouse Y");

        float targetAngle = mouseY > 0 ? upAngleLimt : downAngleLimt;//上下限制2选1
        Quaternion targetRotation = Quaternion.Euler(targetAngle, 0, 0);//构造一个目标的旋转
        //转向目标旋转
        transform.localRotation = Quaternion.RotateTowards
            (transform.localRotation, targetRotation, Mathf.Abs(mouseY) * Time.deltaTime * 60);
    }
}